home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / line.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  4KB  |  229 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "gltk.h"
  29.  
  30. #define CI_OFFSET 16
  31.  
  32. GLenum rgb, doubleBuffer, directRender, windType;
  33.  
  34. GLenum mode1, mode2;
  35. GLint size;
  36. float pntA[3] =
  37. {
  38.   -160.0, 0.0, 0.0
  39. };
  40. float pntB[3] =
  41. {
  42.   -130.0, 0.0, 0.0
  43. };
  44. float pntC[3] =
  45. {
  46.   -40.0, -50.0, 0.0
  47. };
  48. float pntD[3] =
  49. {
  50.   30.0, 60.0, 0.0
  51. };
  52.  
  53. static void Init(void)
  54. {
  55.   GLint i;
  56.  
  57.   glClearColor(0.0, 0.0, 0.0, 0.0);
  58.  
  59.   glLineStipple(1, 0xF0E0);
  60.   glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  61.  
  62.   if (!rgb) {
  63.     for (i = 0; i < 16; i++) {
  64.       tkSetOneColor(i + CI_OFFSET, i / 15.0, i / 15.0, 0.0);
  65.     }
  66.   }
  67.  
  68.   mode1 = GL_FALSE;
  69.   mode2 = GL_FALSE;
  70.   size = 1;
  71. }
  72.  
  73. static void Reshape(int width, int height)
  74. {
  75.  
  76.   glViewport(0, 0, (GLint) width, (GLint) height);
  77.  
  78.   glMatrixMode(GL_PROJECTION);
  79.   glLoadIdentity();
  80.   gluOrtho2D(-175, 175, -175, 175);
  81.   glMatrixMode(GL_MODELVIEW);
  82. }
  83.  
  84. static GLenum Key(int key, GLenum mask)
  85. {
  86.  
  87.   switch (key) {
  88.     case TK_ESCAPE:
  89.       tkQuit();
  90.     case TK_1:
  91.       mode1 = !mode1;
  92.       break;
  93.     case TK_2:
  94.       mode2 = !mode2;
  95.       break;
  96.     case TK_W:
  97.       size++;
  98.       break;
  99.     case TK_w:
  100.       size--;
  101.       if (size < 1) {
  102.     size = 1;
  103.       }
  104.       break;
  105.     default:
  106.       return GL_FALSE;
  107.   }
  108.   return GL_TRUE;
  109. }
  110.  
  111. static void Draw(void)
  112. {
  113.   GLint ci, i;
  114.  
  115.   glClear(GL_COLOR_BUFFER_BIT);
  116.  
  117.   glLineWidth(size);
  118.  
  119.   if (mode1) {
  120.     glEnable(GL_LINE_STIPPLE);
  121.   }
  122.   else {
  123.     glDisable(GL_LINE_STIPPLE);
  124.   }
  125.  
  126.   if (mode2) {
  127.     ci = CI_OFFSET;
  128.     glEnable(GL_LINE_SMOOTH);
  129.     glEnable(GL_BLEND);
  130.   }
  131.   else {
  132.     ci = TK_YELLOW;
  133.     glDisable(GL_LINE_SMOOTH);
  134.     glDisable(GL_BLEND);
  135.   }
  136.  
  137.   glPushMatrix();
  138.  
  139.   glShadeModel(GL_FLAT);
  140.  
  141.   for (i = 0; i < 360; i += 5) {
  142.     glRotatef(5.0, 0, 0, 1);
  143.  
  144.     (rgb) ? glColor3f(1.0, 1.0, 0.0) : glIndexi(ci);
  145.     glBegin(GL_LINE_STRIP);
  146.     glVertex3fv(pntA);
  147.     glVertex3fv(pntB);
  148.     glEnd();
  149.  
  150.     glPointSize(1);
  151.  
  152.     TK_SETCOLOR(windType, TK_GREEN);
  153.     glBegin(GL_POINTS);
  154.     glVertex3fv(pntA);
  155.     glVertex3fv(pntB);
  156.     glEnd();
  157.   }
  158.  
  159.   glPopMatrix();
  160.  
  161.   glFlush();
  162.  
  163.   if (doubleBuffer) {
  164.     tkSwapBuffers();
  165.   }
  166. }
  167.  
  168. static GLenum Args(int argc, char **argv)
  169. {
  170.   GLint i;
  171.  
  172.   rgb = GL_TRUE;
  173.   doubleBuffer = GL_FALSE;
  174.   directRender = GL_TRUE;
  175.  
  176.   for (i = 1; i < argc; i++) {
  177.     if (strcmp(argv[i], "-ci") == 0) {
  178.       rgb = GL_FALSE;
  179.     }
  180.     else if (strcmp(argv[i], "-rgb") == 0) {
  181.       rgb = GL_TRUE;
  182.     }
  183.     else if (strcmp(argv[i], "-sb") == 0) {
  184.       doubleBuffer = GL_FALSE;
  185.     }
  186.     else if (strcmp(argv[i], "-db") == 0) {
  187.       doubleBuffer = GL_TRUE;
  188.     }
  189.     else if (strcmp(argv[i], "-dr") == 0) {
  190.       directRender = GL_TRUE;
  191.     }
  192.     else if (strcmp(argv[i], "-ir") == 0) {
  193.       directRender = GL_FALSE;
  194.     }
  195.     else {
  196.       printf("%s (Bad option).\n", argv[i]);
  197.       return GL_FALSE;
  198.     }
  199.   }
  200.   return GL_TRUE;
  201. }
  202.  
  203. void main(int argc, char **argv)
  204. {
  205.  
  206.   if (Args(argc, argv) == GL_FALSE) {
  207.     tkQuit();
  208.   }
  209.  
  210.   tkInitPosition(0, 0, 300, 300);
  211.  
  212.   windType = (rgb) ? TK_RGB : TK_INDEX;
  213.   windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  214.   windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  215.   tkInitDisplayMode(windType);
  216.  
  217.   if (tkInitWindow("Line Test") == GL_FALSE) {
  218.     tkQuit();
  219.   }
  220.  
  221.   Init();
  222.  
  223.   tkExposeFunc(Reshape);
  224.   tkReshapeFunc(Reshape);
  225.   tkKeyDownFunc(Key);
  226.   tkDisplayFunc(Draw);
  227.   tkExec();
  228. }
  229.